/*
 * ECARX Technology Limited is the owner of the copyright and the trade secret of this software.
 * Without permission, no one has the right to obtain, disclose or use this software in any way.
 */
package com.ecarx.systemui.plugin.ui.statusbar

import android.content.Context
import android.content.Intent
import android.os.UserHandle
import android.util.AttributeSet
import android.view.View
import androidx.core.content.res.ResourcesCompat
import com.ecarx.systemui.plugin.R
import com.ecarx.systemui.plugin.controller.MessageController
import com.ecarx.systemui.plugin.ui.custom.SystemBarButton
import com.ecarx.systemui.plugin.utils.IntentUtils
import com.ecarx.systemui.plugin.utils.LogUtils

class MessageView : SystemBarButton {
    private val TAG = "MessageView"
    constructor(context: Context?) : this(context!!, null) {}
    constructor(context: Context?, attrs: AttributeSet?) : this(context!!, attrs, 0) {}
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context!!,
        attrs,
        defStyleAttr
    ) {

    }

    override fun getContentDescriptionString(): String {
        return "MessageView"
    }

    override fun initView() {
        super.initView()
        setMessageView()
        MessageController.getInstance(context).setOnNotifyMessageIconChangeListener {
            setMessageView()
        }

        setOnClickListener { v: View? ->
            LogUtils.i(TAG, "onMessageViewClick - ControlBoard_page")
            var intent = Intent()
            intent.action = IntentUtils.ACTION_MESSAGE
            intent.putExtra("ecarx.controlboard.EXTRA_CURRENT_PAGE", 1)
            context.sendBroadcastAsUser(intent, UserHandle.ALL)
        }
    }


    private fun setMessageView() {
        var messageCount = MessageController.getInstance(context).messageCount
        LogUtils.i(TAG, "setMessageView messageCount = $messageCount")
        if (messageCount > 0) {
            setImageDrawable(
                ResourcesCompat
                    .getDrawable(
                        resources,
                        R.drawable.ic_status_message_with_red,
                        context.theme
                    )
            )
        } else {
            setImageDrawable(
                ResourcesCompat
                    .getDrawable(
                        resources,
                        R.drawable.ic_status_message,
                        context.theme
                    )
            )
        }
    }

    override fun onFinishInflate() {
        super.onFinishInflate()

    }


}